package com.yst.shader.feature.ss.effect;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;

import com.facebook.drawee.generic.GenericDraweeHierarchy;


public class SSImageView extends ShimmerImageView {
    private Drawable mDrawable;
    private Rect mOffset;
    protected boolean mShadowFocused;
    private boolean mEnableBackground;
    private boolean mEnableShimmer;
    private Rect mNinePatchPaddingRect;


    public SSImageView(Context context, GenericDraweeHierarchy hierarchy) {
        super(context, hierarchy);
    }

    public SSImageView(Context context) {
        super(context);
    }

    public SSImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SSImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SSImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mEnableBackground && mShadowFocused && mDrawable != null) {
            mDrawable.draw(canvas);
        }
        super.onDraw(canvas, mEnableShimmer);
    }

    public void config(ImageConfig config) {
        if (config != null) {
            Context context = getContext();
            mDrawable = config.getShadow(context);
            if (mDrawable != null && mDrawable instanceof NinePatchDrawable) {
                if (mNinePatchPaddingRect == null) {
                    mNinePatchPaddingRect = new Rect();
                }
                mDrawable.getPadding(mNinePatchPaddingRect);
            }
            mOffset = config.getOffset();
            mEnableBackground = config.isEnableBackground();
            if (mEnableBackground) {
                setLayerType(LAYER_TYPE_NONE, null);
            }
            mEnableShimmer = config.isEnableShimmer();
        } else {
            mDrawable = null;
            mOffset = null;
            mEnableBackground = false;
            mEnableShimmer = false;
            if (mNinePatchPaddingRect != null) {
                mNinePatchPaddingRect.setEmpty();
            }
        }
    }

    public void start() {
        mShadowFocused = true;
        if (mEnableBackground && mDrawable != null) {
            int w = getWidth();
            int h = getHeight();
            if (mDrawable instanceof NinePatchDrawable) {
                mDrawable.setBounds(-mNinePatchPaddingRect.left + getPaddingLeft(), -mNinePatchPaddingRect.top + getPaddingTop(),
                        w + mNinePatchPaddingRect.right - getPaddingRight(), h + mNinePatchPaddingRect.bottom - getPaddingRight());
            } else {
                if (mOffset != null) {
                    mDrawable.setBounds(-mOffset.left + getPaddingLeft(), -mOffset.top + getPaddingTop(),
                            w + mOffset.right - getPaddingRight(), h + mOffset.bottom - getPaddingBottom());
                }
            }
        }
        if (mEnableShimmer) {
            shimmer();
            startShimmer();
        }
    }

    public void stop() {
        mShadowFocused = false;
        if (mEnableShimmer) {
            stopShimmer();
        }
        postInvalidate();
    }
}
